package com.yunhaikeji.api;


import com.hik.AlarmService.AlarmDemo;
import com.hik.CmsService.CmsDemo;
import com.hik.SsService.SsDemo;
import com.hik.Boot.IsupBoot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@SpringBootApplication(scanBasePackages = {"com.yunhaikeji"})
@MapperScan({"com.yunhaikeji.api.module.**.mapper","com.yunhaikeji.api.module.**.dao"})
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 1800)
@EnableSwagger2
@EnableScheduling
public class ApiApplication extends SpringBootServletInitializer {

    public static List<Integer> lLoginIDs = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        ApplicationContext app = SpringApplication.run(ApiApplication.class, args);
        IsupBoot test = new IsupBoot();
        //初始化报警服务
        AlarmDemo alarmDemo = new AlarmDemo();
        alarmDemo.eAlarm_Init();
        alarmDemo.startAlarmListen();
        //初始化存储服务
        SsDemo ssDemo = new SsDemo();
        ssDemo.eSS_Init();
        ssDemo.startSsListen();
        //初始化流媒体服务
/*        StreamDemo streamDemo=new StreamDemo();
        streamDemo.eStream_Init();*/

        //初始化语音流媒体服务
/*        VoiceDemo voiceDemo=new VoiceDemo();
        voiceDemo.voice_Init();
        voiceDemo.startVoiceServeListen();*/

        //初始化注册服务
        CmsDemo cmsDemo = new CmsDemo();
        cmsDemo.cMS_Init();
        cmsDemo.startCmsListen();

        /**
         * 上传图片至存储服务器
         */
//        ssDemo.ssCreateClient();
//        String picUrl = ssDemo.ssUploadPic();
//        ssDemo.ssDestroyClient();

        //等待设备注册上线,单独调试存储服务不需要此步骤
        while (lLoginIDs.size() == 0) {
            Thread.sleep(10000);
        }
//        cmsDemo.CMS_ISAPIPassThrough_Search();
//        cmsDemo.CMS_ISAPIPassThrough_FDLib();
//        cmsDemo.CMS_ISAPIPassThrough_UpdateFace(picUrl);

//        cmsDemo.CMS_ISAPIPassThrough_AddPerson();
//        cmsDemo.CMS_ISAPIPassThrough_DelPerson();

        /**
         * ISUP透传接口
         */
//        cmsDemo.CMS_ISAPIPassThrough();
//        cmsDemo.CMS_XMLRemoteControl();

        /**
         * 取流预览模块
         */
/*        JPanelDemo.jRealWinInit();
        streamDemo.startRealPlayListen();
        streamDemo.RealPlay(1);
        Thread.sleep(20000);
        streamDemo.StopRealPlay();
        //关闭预览窗口
        JPanelDemo.jRealWinDestory();*/

        /**
         * ISUP5.0接入按时间回放取流模块
         */
/*        JPanelDemo.jRealWinInit();
        streamDemo.startPlayBackListen();
        //开启回放预览
        streamDemo.PlayBackByTime(1);
        Thread.sleep(20000);
        //停止回放预览
        streamDemo.stopPlayBackByTime();*/

        /**
         * ISUP5.0语音转发模块，需要确定设备是否支持此功能
         */
/*        voiceDemo.StartVoiceTrans();
        voiceDemo.StopVoiceTrans();*/
        while (true) {
            //这里加入控制台输入控制，是保证服务一直运行，当输入Y表示停止服务
            System.out.print("请选择是否注销服务(Y/N)：");
            Scanner input = new Scanner(System.in);
            String str = input.next();
            if (str.equals("Y")) {
                break;
            }
        }
        //停止监听释放SDK
        if (CmsDemo.CmsHandle >= 0) {
            System.out.println("停止注册CMS服务");
            CmsDemo.hCEhomeCMS.NET_ECMS_StopListen(CmsDemo.CmsHandle);
            CmsDemo.hCEhomeCMS.NET_ECMS_Fini();
        }
        if (AlarmDemo.AlarmHandle >= 0) {
            System.out.println("停止报警Alarm服务");
            AlarmDemo.hcEHomeAlarm.NET_EALARM_StopListen(AlarmDemo.AlarmHandle);
            AlarmDemo.hcEHomeAlarm.NET_EALARM_Fini();
        }
        if (SsDemo.SsHandle >= 0) {
            System.out.println("停止存储SS服务");
            SsDemo.hCEhomeSS.NET_ESS_StopListen(SsDemo.SsHandle);
            SsDemo.hCEhomeSS.NET_ESS_Fini();
        }
        /*if (StreamDemo.StreamHandle >= 0) {
            System.out.println("停止流媒体Stream服务");
            StreamDemo.hCEhomeStream.NET_ESTREAM_StopListenPreview(StreamDemo.StreamHandle);
            StreamDemo.hCEhomeStream.NET_ESTREAM_Fini();
        }
        if (StreamDemo.m_lPlayBackListenHandle >= 0) {
            System.out.println("停止回放流媒体Stream服务");
            StreamDemo.hCEhomeStream.NET_ESTREAM_StopListenPlayBack(StreamDemo.m_lPlayBackListenHandle);
            StreamDemo.hCEhomeStream.NET_ESTREAM_Fini();
        }
        if (VoiceDemo.VoicelServHandle >= 0) {
            System.out.println("停止语音流媒体VoiceStream服务");
            VoiceDemo.hCEhomeVoice.NET_ESTREAM_StopListenVoiceTalk(VoiceDemo.VoicelServHandle);
            VoiceDemo.hCEhomeVoice.NET_ESTREAM_Fini();
        }*/
        return;
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ApiApplication.class);
    }

}
